package aenbike.main;

import com.google.android.maps.MyLocationOverlay;

import aenbike.main.maputilities.MyLocater;
import aenbike.main.model.datacontext.AplicationDataContext;
import aenbike.main.model.entities.Point;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class AenBike extends Activity implements LocationListener {

	private WebView browser;
	LocationManager locationManager;
	MyLocater locater = new MyLocater();
	
	//database objects
	private AplicationDataContext dataContext;


	@Override
	protected void onDestroy() {
		super.onDestroy();
		// cut location service requests
		locationManager.removeUpdates(this);
	}
	
	@Override
	public void onPause()
	{
		
	}

	@Override
	public void onResume()
	{
		getLocation();
	}
	private void getLocation() {
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE); // use GPS (you must be outside)
		// criteria.setAccuracy(Criteria.ACCURACY_COARSE); // towers, wifi
		String provider = locationManager.getBestProvider(criteria, true);
	
		// In order to make sure the device is getting the location, request
		// updates [wakeup after changes of: 1 sec. or 0 meter]
		locationManager.requestLocationUpdates(provider, 1, 0, this);
		locater.setNewLocation(locationManager.getLastKnownLocation(provider));
	}
	
	// it creates the menu
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.mapmenu, menu);
	    return true;
	}

	// selecting menu option
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.exit:
	            finish();
	            return true;
	        case 2: //R.id.menu_main_exit:
	            //showHelp();
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		getLocation();
		setupbrowser();
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	
		//Database connection
		try{
			dataContext = new AplicationDataContext(this);
			populateDataBase();
		} catch(Exception e){
			Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
		}
	
	}
	
	/**
	 * DataBase needed
	 */
	private void populateDataBase(){
		if(dataContext != null){
			//Point point = new Point();
			//point.
		}
		
	}

	/** Sets up the browser object and loads the URL of the page **/
	private void setupbrowser() {
		final String centerURL = "javascript:centerAt(" + locater.getLatitude()
				+ "," + locater.getLongitude() + ")";
		// set up the browser to show location results
		browser = (WebView) findViewById(R.id.webview);
		browser.getSettings().setJavaScriptEnabled(true);
		browser.addJavascriptInterface(locater, "locater");
		browser.loadUrl("file:///android_asset/webMap.html");

		// Wait for the page to load then send the location information
		browser.setWebViewClient(new WebViewClient() {
			@Override
			public void onPageFinished(WebView view, String url) {
				browser.loadUrl(centerURL);
			}
		});
	}

	@Override
	public void onLocationChanged(Location location) {
		//String lat = String.valueOf(location.getLatitude());
		//String lon = String.valueOf(location.getLongitude());
		//Toast.makeText(getApplicationContext(), lat + "\n" + lon, 1).show();
		locater.setNewLocation(location);
	}

	@Override
	public void onProviderDisabled(String provider) {
		// needed by Interface. Not used
	}

	@Override
	public void onProviderEnabled(String provider) {
		// needed by Interface. Not used
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// needed by Interface. Not used
	}

}